package com.example.serialportdemo;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.BRMicro.NETLH_E;

public class LoginActivity extends AppCompatActivity {
    private TextView textView;
    private static NETLH_E netlh_e=NETLH_E.getInstance();
    private boolean flag;
    private void setCancel(boolean cancel){
        this.flag=cancel;
    }
    private  void makeToastByRunOnUiThread(final String str)//更新UI
    {
        LoginActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(str);
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        textView=findViewById(R.id.tv_login);
        thread.start();
    }

    Thread thread=new Thread(new Runnable() {
        @Override
        public void run() {
            setCancel(false);
            int[] CmdGetRedressImage_ErrFlag={-1};
            int[] CmdGenChar_ErrFlag={-1};
            int[] CmdSearchChar_ErrFlag={-1};
            int[] CmdSearchChar_RetMbIndex={-1};
            int[] CmdSearchChar_RetScore={-1};
            makeToastByRunOnUiThread("开始录入指纹");
            while (!flag){
                int i=netlh_e.CmdGetRedressImage(0,CmdGetRedressImage_ErrFlag);//获取指纹图像
                Log.d("register",i+"");
                if(CmdGetRedressImage_ErrFlag[0]==0){
                    setCancel(true);
                    netlh_e.CmdGenChar(1,CmdGenChar_ErrFlag);
                    if(CmdGenChar_ErrFlag[0]==0){
                        netlh_e.CmdSearchChar(1,CmdSearchChar_RetMbIndex,CmdSearchChar_RetScore,CmdSearchChar_ErrFlag);
                        if(CmdSearchChar_ErrFlag[0]==0) {
                            makeToastByRunOnUiThread("匹配到用户指纹，索引为："+CmdSearchChar_RetMbIndex[0]);
                        }
                        else{
                            makeToastByRunOnUiThread("未搜索到该用户");
                        }
                    }
                    else{
                        makeToastByRunOnUiThread("登陆失败");
                    }
                }
                else{
                    CmdGetRedressImage_ErrFlag[0]=-1;
                }
            }
            return;
        }
    });
}